package com.zsx.shopping.cart.controller;

import com.zsx.shopping.cart.service.CartService;
import com.zsx.shopping.common.api.R;
import com.zsx.shopping.common.api.cart.vo.CartVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @GetMapping("/cart")
    public R<CartVO> getCart() {
        return R.success(cartService.getCart());
    }

    @DeleteMapping("/cart")
    public R<Void> clearCart() {
        cartService.clearCart();
        return R.success();
    }

    @PostMapping("/item")
    public R<CartVO> addToCart(@RequestParam Long skuId, @RequestParam Integer count) {
        cartService.addToCart(skuId, count);
        return R.success(cartService.getCart());
    }

    @PutMapping("/count")
    public R<CartVO> changeCount(@RequestParam Long skuId, @RequestParam Integer count) {
        cartService.changeCount(skuId, count);
        return R.success(cartService.getCart());
    }

    @DeleteMapping("/item")
    public R<CartVO> deleteItem(@RequestParam Long skuId) {
        cartService.delete(skuId);
        return R.success(cartService.getCart());
    }

    @PutMapping("/cart")
    public R<Void> updateCart() {
        cartService.updateCart();
        return R.success();
    }

    @PostMapping("/toTrade")
    public R<String> toTrade() {
        return R.success(cartService.toTrade());
    }
}
